﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Database;

public partial class cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["tenkh"] == null)
            {
                image_dathang.Enabled = false;
                lb_error.Visible = true;
                lb_error.Text = "Bạn vui lòng đăng nhập trước khi đặt hàng";
            }
            else
            {
                image_dathang.Enabled = true;
                lb_error.Visible = false;
            }
            Hien_thi_gio_hang();

        }

    }
    void Hien_thi_gio_hang()
    {
        if (Session["ShoppingCart"] != null)
        {
            ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
            //DataTable Gio_hang = (DataTable)Session["Cart"];
            gv_giohang.DataSource = cart;
            gv_giohang.DataBind();
            double kq = Tinh_tri_gia();
            lb_tongtien.Text = kq.ToString("#,### VNĐ");
        }
    }

    // ham tinh tri
    double Tinh_tri_gia()
    {
        ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
        double kq = 0;
        foreach (ShoppingCart.Item item in cart)
        {
            kq +=item.Total;
        }
        return kq;
    }


    protected void image_dathang_Click(object sender, ImageClickEventArgs e)
    {

        ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
        if (cart.Count > 0)
        {
            DonDatHang ddh = new DonDatHang();
            ddh.Tenkh = Session["tenkh"].ToString();
            ddh.Ngaydathang = DateTime.Now;
            ddh.Tinhtrang = false;
            ddh.ThemDonDatHang(ddh);

            // Lấy mã đơn đặt hàng mới nhất
            DataTable bang = ddh.DSDDHMoiNhat();
            int maddh1 = int.Parse(bang.Rows[0]["maddh"].ToString());
            foreach (ShoppingCart.Item item in cart.ToList())
            {
                CT_DonDatHangLK ctlk = new CT_DonDatHangLK();
                ctlk.Maddh = maddh1;
                ctlk.MaLinhKien = item.ID;
                ctlk.SoLuong = item.Quantity;
                ctlk.GiaBan = item.Cost;
                LinhKien lk = new LinhKien();
                int malinhkien = item.ID;
                bool kq = lk.KiemTraLinhKien(malinhkien);
          
                if (kq == true)
                {
                    ctlk.ThemCT_DonDatHangLK(ctlk);
                }
                else
                {
                    CT_DonDatHang ct = new CT_DonDatHang();
                    ct.Maddh = maddh1;
                    ct.Mamay = item.ID;
                    ct.Soluong = item.Quantity;
                    ct.Giaban = item.Cost;
                    ct.ThemCT_DonDatHang(ct);
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "a", "alert('Chúc mừng ! Bạn đã đặt hàng thành công');", true);
            Session["ShoppingCart"] = null;
            string links = Request.ServerVariables["HTTP_REFERER"].ToString();
            Response.Redirect(links);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "a", "alert('Bạn phải chọn hàng trước khi đặt');", true);

        }
    }



    protected void gv_giohang_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv_giohang.EditIndex = e.NewEditIndex;
        Hien_thi_gio_hang();
    }

    protected void gv_giohang_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //DataTable Gio_hang = (DataTable)Session["Cart"];
        ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
        int mh = int.Parse(gv_giohang.DataKeys[e.RowIndex][0].ToString());
        foreach (ShoppingCart.Item item in cart.ToList())
        {
            if (item.ID == mh)
            {
                 string a = ((TextBox)gv_giohang.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
                 if (XuLyThamSo.KTSo(a) == true)
                 {
                     
                     if (int.Parse(a) != 0 && int.Parse(a) >= 1)
                     {
                         int sl = Convert.ToInt32(((TextBox)gv_giohang.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
                         item.Total = sl*item.Total;
                         item.Quantity = sl;
                         gv_giohang.EditIndex = -1;
                         cart.UpdateItem(item);
                         Session["ShippingCart"] = cart;
                         Hien_thi_gio_hang();
                         break;
                     }
                     else
                     {
                         ClientScript.RegisterStartupScript(this.GetType(), "a", "alert('Số lượng phải là số nguyên dương');", true);

                     }
                 }
                 else {
                     ClientScript.RegisterStartupScript(this.GetType(), "a", "alert('Hãy nhập số !');", true);
                 }
            }
        } 
        

    }

    protected void gv_giohang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
        int mh = int.Parse(gv_giohang.DataKeys[e.RowIndex][0].ToString());
        int i = 0;
        foreach (ShoppingCart.Item item in cart.ToList())
        {
            if (item.ID == mh)
            {
                //cart.RemoveItem(item);
                cart.RemoveAt(i); 
                Session["ShippingCart"] = cart;
                break;
            }
            i++;
        }
        string links = Request.ServerVariables["HTTP_REFERER"].ToString();
        Response.Redirect(links);
    }



    protected void gv_giohang_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv_giohang.EditIndex = -1;//
        Hien_thi_gio_hang();
    }

    protected void gv_giohang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_giohang.PageIndex = e.NewPageIndex;
        Hien_thi_gio_hang();
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {

    }
}